package redis

import (
	"context"
	"fmt"
	"srv/cart"
	"strconv"
)

func AddCart(cart map[string]interface{}) error {
	key := fmt.Sprintf("cart:%d:%d", cart["user_id"], cart["good_id"])
	return Rdb.HMSet(context.Background(), key, cart).Err()
}
func CartExist(userId, goodId int64) bool {
	key := fmt.Sprintf("cart:%d:%d", userId, goodId)
	return Rdb.Exists(context.Background(), key).Val() == 1
}
func UpdateCart(userId, goodId, num int64) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodId)
	return Rdb.HSet(context.Background(), key, "num", num).Err()
}
func CartDel(userId, goodId int64) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodId)
	return Rdb.Del(context.Background(), key).Err()
}
func DetailCleve(userId, goodId int64) (detail []*cart.Detail, err error) {
	key := fmt.Sprintf("cart:%d:%d", userId, goodId)
	det := Rdb.HGetAll(context.Background(), key).Val()
	userid, _ := strconv.Atoi(det["user_id"])
	goodprice, _ := strconv.Atoi(det["good_price"])
	goodStock, _ := strconv.Atoi(det["num"])
	goodstate, _ := strconv.Atoi(det["good_state"])
	goodtitle := det["good_title"]

	detail = append(detail, &cart.Detail{
		UserId:    int64(userid),
		GoodTitle: goodtitle,
		GoodPrice: uint64(goodprice),
		GoodStock: int64(goodStock),
		GoodState: int64(goodstate),
	})
	return
}
func StateCart(userId, state int64) (err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {

		Rdb.HSet(context.Background(), v, "good_state", state)
	}
	return
}
func CleveCart(userId int64) (err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		Rdb.Del(context.Background(), v)
	}
	return
}
func CountCart(userId int64) (count int64, err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		det := Rdb.HGetAll(context.Background(), v).Val()
		num, _ := strconv.Atoi(det["num"])
		count += int64(num)
	}
	return
}
func AmountCart(userId int64) (amount float64, err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		det := Rdb.HGetAll(context.Background(), v).Val()
		num, _ := strconv.Atoi(det["num"])
		price, _ := strconv.ParseFloat(det["good_price"], 64)
		amount += price * float64(num)
	}
	return
}
func ListCart(userId int64) (list []*cart.List, err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		det := Rdb.HGetAll(context.Background(), v).Val()
		userid, _ := strconv.Atoi(det["user_id"])
		goodprice, _ := strconv.Atoi(det["good_price"])
		goodStock, _ := strconv.Atoi(det["good_stock"])
		goodstate, _ := strconv.Atoi(det["good_state"])
		goodtitle := det["good_title"]
		goodcover := det["good_cover"]
		stiate, _ := strconv.Atoi(det["good_state"])
		if stiate == 1 {
			list = append(list, &cart.List{
				UserId:    int64(userid),
				GoodTitle: goodtitle,
				GoodCover: goodcover,
				GoodPrice: uint64(goodprice),
				GoodStock: int64(goodStock),
				GoodState: int64(goodstate),
			})
		}

	}
	return
}
